import math
from math import degrees, radians

import random
from random import randint
from random import randint as ri

import pyglet
from pyglet.gl import  *
from pyglet.window import key

import cocos
from cocos.actions import *
from cocos.layer import *
from cocos.scene import *
from cocos.director import director
from cocos.scenes.transitions import *
from cocos.sprite import Sprite

import Box2D as box2d

import prop
import box
import weapon
from prop import *
from res import Images
from globalvars import *

       
class BaseMap(Layer):
    "The arena~"
    def __init__(self, world, *args, **kwargs):
        "Init"
        super(BaseMap,self).__init__( *args, **kwargs)
        self.world = world
        self.add(self.MapBG())
        
    def update(self,dt):
        pass
    
    class MapBG(ColorLayer):
        "Game Background"
        def __init__(self):
            ColorLayer.__init__(self,255,255,255,0)
            
        def update(self,dt):
            pass
        

class Map1(BaseMap):
    "The basic arena"
    RATIO = 30.0
    WIDTH = Images.photon_bg.width*4
    HEIGHT = Images.photon_bg.height*4
    B2WIDTH = WIDTH/RATIO
    B2HEIGHT = HEIGHT/RATIO
    SIZE = (WIDTH,HEIGHT)
    def __init__(self, world, *args, **kwargs):
        super(Map1,self).__init__(world, *args, **kwargs)
        
        "playerBox"
        self.playerBox = box.PlayerBox(self.world, Images.box)
        self.playerBox.body.position = (0/RATIO, 0/RATIO)
        self.playerBox.color = (100,255,50)
        self.world.addEntity(self.playerBox,z=3)
        self.world.addEntity(self.playerBox.crossHair,z=4)
        #self.world.add(self.playerBox.boxBlur,z=4)
        
        "Walls"
        wallbodyDef = box2d.b2BodyDef()
        wallbodyDef.linearDamping = 0.6
        wallbodyDef.angularDamping = 0.6
        
        horiWallShapeDef = box2d.b2PolygonDef()
        horiWallShapeDef.SetAsBox(self.B2WIDTH/2, 5/RATIO, (0,0),0)
        horiWallShapeDef.friction = 0.5
        horiWallShapeDef.restitution = 0.6
        horiWallShapeDef.density = 0.3                                                                                                     
    
        hcoords = [  (box2d.b2Vec2(0/RATIO, self.B2HEIGHT/2), radians(0)),
                    (box2d.b2Vec2(0/RATIO, -self.B2HEIGHT/2), radians(0)) ]
    
        for pos, angle in hcoords:
            body = self.world.CreateBody(wallbodyDef)
            body.position = pos
            body.angle = angle
            shape = body.CreateShape(horiWallShapeDef)
        
        vertWallShapeDef = box2d.b2PolygonDef()
        vertWallShapeDef.SetAsBox(5/RATIO, self.B2HEIGHT/2, (0,0),0)
        vertWallShapeDef.friction = 0.5
        vertWallShapeDef.restitution = 0.6
        vertWallShapeDef.density = 0.3 
        
        vcoords = [(box2d.b2Vec2(self.B2WIDTH/2, 0/RATIO), radians(0)),
                    (box2d.b2Vec2(-self.B2WIDTH/2, 0/RATIO), radians(0)) ]
        
        for pos, angle in vcoords:
            body = self.world.CreateBody(wallbodyDef)
            body.position = pos
            body.angle = angle
            shape = body.CreateShape(vertWallShapeDef)
        
        "Bumpers"
        for i in xrange(7):
            bumper = prop.Bumper(self.world)
            bumper.bumperBody.position = box2d.b2Vec2( 
                                    ri(int(20/RATIO), int(self.B2WIDTH-(20/RATIO))), 
                                    ri(int(20/RATIO), int(self.B2HEIGHT-(20/RATIO))) )
            bumper.bumperBody.position -= box2d.b2Vec2(self.B2WIDTH/2, self.B2HEIGHT/2)
            bumper.centerBody.position = bumper.bumperBody.position
            bumper.initJoint()
            #bumper.color = (ri(0,155)+100,ri(0,155)+100,ri(0,155)+100)
            self.world.addEntity(bumper)
        
        "Moveable Bars"
        for i in xrange(8):
            mb = prop.MoveableBar(self.world)
            mb.body.position = box2d.b2Vec2( 
                                    ri(int(20/RATIO), int(self.B2WIDTH-(20/RATIO))), 
                                    ri(int(20/RATIO), int(self.B2HEIGHT-(20/RATIO))) )
            mb.body.position -= box2d.b2Vec2(self.B2WIDTH/2, self.B2HEIGHT/2)
            mb.body.angle = degrees(ri(0,36))
            mb.color = (ri(0,155)+100,ri(0,155)+100,ri(0,155)+100)
            self.world.addEntity(mb)
            
        "Free weapons"
        sar = weapon.SemiAutoRifle(self.world)
        sar.body.position = (-400/RATIO, 100/RATIO)
        self.world.addEntity(sar)
        
        sar2 = weapon.SemiAutoRifle(self.world)
        sar2.body.position = (40/RATIO, 100/RATIO)
        sar2.color = (0,100,255)
        self.world.addEntity(sar2)
        
            
    def addRandomProp(self,prop):
        pass
    
    class MapBG(BaseMap.MapBG):
        "Game Background"
        def __init__(self,scale=1):
            BaseMap.MapBG.__init__(self)
            self.image= Sprite(Images.photon_bg,)
            self.image.scale = 4
            self.image.position = (0,0)#(pos[0]/2,pos[1]/2)
            self.add(self.image)
        
        def update(self,dt):
            pass
            
